package huriezy.quickcontacts.activities;

import huriezy.quickcontacts.R;
import huriezy.quickcontacts.activities.fragments.FacebookPickerFragment;
import huriezy.quickcontacts.activities.fragments.UserScreenFragment;
import huriezy.quickcontacts.activities.interfaces.ITerminableListener;
import huriezy.quickcontacts.models.LogDebug;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class UserSelectionFragmentActivity extends FragmentActivity implements ITerminableListener {

	public static final String	FACEBOOK_SOCIAL_TYPE	= "facebook_social_type";
	public static final String	GOOGLEPLUS_SOCIAL_TYPE	= "googleplus_social_type";

	public static final String	FACEBOOK_PICKER_TAG		= "facebookpickertag";
	public static final String	GOOGLEPLUS_PICKER_TAG	= "googlepluspickertag";
	public static final String	USER_SCREEN_TAG			= "userscreentag";

	// Intent id
	public static final String	USED_SOCIAL_KEY			= "used_social_type";
	public static final String	SELECTED_USER			= "selected_user";


	private FacebookPickerFragment	facebookUserPickerFragment;
	private UserScreenFragment	userScreenFragment;
	private String				currentType;

	private Bundle				selectedUser			= null;
	private int					requestCode				= -1;

	@Override
	protected void onCreate(Bundle args) {
		super.onCreate(args);
		setContentView(R.layout.activity_user_selection_fragment);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = getIntent();
		if (intent != null) {
			String usedSocialFragment = intent.getStringExtra(USED_SOCIAL_KEY);
			intent.getExtras().getInt("requestCode");
			if (usedSocialFragment != null && usedSocialFragment.length() > 0) {
				if (usedSocialFragment.equals(FACEBOOK_SOCIAL_TYPE)) {
					selectFragment(FACEBOOK_PICKER_TAG);
				}
			}
		}
	}

	/**
	 * 
	 * @param tag
	 */
	private void selectFragment(String tag) {
		final FragmentManager fragmentManager = getSupportFragmentManager();
		Fragment currentFragment = null;
		if (tag == FACEBOOK_PICKER_TAG) {
			FacebookPickerFragment pickerFragment = (FacebookPickerFragment) fragmentManager.findFragmentByTag(tag);
			if (pickerFragment == null) {
				pickerFragment = new FacebookPickerFragment();
				pickerFragment.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
						Object itemAtPosition = adapter.getItemAtPosition(position);
						if (itemAtPosition instanceof Bundle) {
							selectedUser = (Bundle) itemAtPosition;
							selectFragment(USER_SCREEN_TAG);
						}
						else {
							LogDebug.info("Retrieved object : " + itemAtPosition, getClass());
						}
					}
				});
			}
			facebookUserPickerFragment = pickerFragment;
			currentFragment = facebookUserPickerFragment;
			currentType = FACEBOOK_SOCIAL_TYPE;

			// Load data for user picker
			Intent parentIntent = getIntent();
			String contactName = null;
			if (parentIntent != null) {
				contactName = parentIntent.getExtras().getString(FacebookPickerFragment.SEARCH_NAME_DATA);
			}
			facebookUserPickerFragment.setContactName(contactName);
		}
		else if (tag == USER_SCREEN_TAG) {
			UserScreenFragment screenFragment = (UserScreenFragment) fragmentManager.findFragmentByTag(USER_SCREEN_TAG);
			String screenType = null;
			if (currentType == FACEBOOK_SOCIAL_TYPE) {
				screenType = FACEBOOK_SOCIAL_TYPE;
			}
			if (screenFragment == null) {
				if (screenType != null && selectedUser != null) {
					screenFragment = UserScreenFragment.newInstance(screenType, selectedUser);
				}
			}
			else {
				screenFragment.setSelectedUser(selectedUser);
				screenFragment.setScreenType(screenType);
			}
			screenFragment.setOnTerminateListener(this);
			userScreenFragment = screenFragment;
			currentFragment = userScreenFragment;
		}
		else {
			LogDebug.error("Cannot handle donate fragment", this);
		}

		if (currentFragment != null) {
			final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
			fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
			fragmentTransaction.replace(R.id.picker_fragment, currentFragment);
			fragmentTransaction.commit();
		}
	}

	/**
	 * 
	 * @param error
	 */
	public void onFacebookError(Exception error) {
		onError(error.getLocalizedMessage(), false);
	}

	/**
	 * 
	 * @param error
	 * @param finishActivity
	 */
	private void onError(String error, final boolean finishActivity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.facebook_error_title_text);
		builder.setMessage(error);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {
				if (finishActivity) {
					onFinishActivity(null);
				}
			}
		});
		builder.show();
	}

	/**
	 * 
	 */
	public void onFinishActivity(Bundle selectedUser) {
		Intent intent = null;
		if (currentType == FACEBOOK_SOCIAL_TYPE) {
			intent = new Intent();
			if (selectedUser != null) {
				intent.putExtra(SELECTED_USER, selectedUser);
				intent.putExtra("requestCode", requestCode);
			}
		}
		if (intent != null) {
			setResult(RESULT_OK, intent);
		}
		else {
			setResult(RESULT_CANCELED, null);
		}
		finish();
	}
}
